package dobest.ray.com.jiongjiong.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import dobest.ray.com.jiongjiong.R;

/**
 * Created by Ray on 2016/3/16.
 * https://github.com/ray0807
 * 首页视力状态图
 */
public class OvalChartView extends RelativeLayout {
    private Context context;
    private RectF mOval;
    private Paint mPaint;
    private Paint mPaintText;
    private float mStrokeWidth = 16f;

    private Shader mSweepGradient = null;
    private Bitmap bitmapOk;
    private Bitmap bitmapDanger;
    private float radius;
    private float offsetAnger = 30.0f;
    private float angerLeft = 100f;//0-150
    private float angerRight = 70f;//0-150

    private int mBitWidthOk = 0;
    private int mBitHeightOk = 0;
    private int mBitWidthDanger = 0;
    private int mBitHeightDanger = 0;
    private int padding = 20;


    public OvalChartView(Context context) {
        super(context);
        this.context = context;
        init();
    }


    public OvalChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public OvalChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint.setColor(context.getResources().getColor(R.color.colorPrimary));
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaintText = new Paint();
        mPaintText.setAntiAlias(true);
        mPaintText.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaintText.setStrokeWidth(1);
        mPaintText.setTextSize(40);
        mPaintText.setColor(context.getResources().getColor(R.color.grey));
        bitmapOk = BitmapFactory.decodeResource(context.getResources(), R.mipmap.img_is_ok_point);
        bitmapDanger = BitmapFactory.decodeResource(context.getResources(), R.mipmap.img_is_danger_point);
        mBitWidthOk = bitmapOk.getWidth();
        mBitHeightOk = bitmapOk.getHeight();
        mBitWidthDanger = bitmapDanger.getWidth();
        mBitHeightDanger = bitmapDanger.getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mSweepGradient = new SweepGradient(mOval.centerX(), mOval.centerY(), new int[]{context.getResources().getColor(R.color.homepage_color_yellow), context.getResources().getColor(R.color.homepage_color_yellow),
                context.getResources().getColor(R.color.colorPrimary), context.getResources().getColor(R.color.homepage_color_yellow), context.getResources().getColor(R.color.homepage_color_yellow)}, new float[]{0.25f, 0.5f, 0.75f, 1f, 0.25f});
        mPaint.setShader(mSweepGradient);
        canvas.drawArc(mOval, 120, 300, false, mPaint);
        canvas.drawBitmap(bitmapOk, (float) (mOval.centerX() - radius * Math.sin((angerLeft + offsetAnger) / 180.0f * Math.PI) - (mBitWidthOk - mStrokeWidth) / 2.0f), (float) (mOval.centerY() + radius * Math.cos((angerLeft + offsetAnger) / 180.0f * Math.PI) - (mBitHeightOk) / 2.0f), null);
        canvas.drawBitmap(bitmapDanger, (float) (mOval.centerX() + radius * Math.sin((angerRight + offsetAnger) / 180.0f * Math.PI) - (mBitWidthDanger + mStrokeWidth) / 2.0f), (float) (mOval.centerY() + radius * Math.cos((angerRight + offsetAnger) / 180.0f * Math.PI) - (mBitHeightDanger) / 2.0f), null);
        canvas.drawText("0.1", (float) (mOval.centerX() - radius * Math.sin((offsetAnger) / 180.0f * Math.PI) - (mStrokeWidth) / 2.0f)-4, getHeight() - 20, mPaintText);
        canvas.drawText("2.0", (float) (mOval.centerX() + radius * Math.sin((offsetAnger) / 180.0f * Math.PI) - ( mStrokeWidth) / 2.0f)-mPaintText.measureText("2.0")/2, getHeight() - 20, mPaintText);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        updateOval();
    }

    private void updateOval() {
        int xp = getPaddingLeft() + getPaddingRight() + padding;
        int yp = getPaddingBottom() + getPaddingTop() + padding;
        mOval = new RectF(getPaddingLeft() + mStrokeWidth + padding, getPaddingTop()
                + mStrokeWidth + padding, getPaddingLeft() + (getWidth() - xp)
                - mStrokeWidth, getPaddingTop() + (getHeight() - yp)
                - mStrokeWidth);
        radius = mOval.centerX() - mStrokeWidth / 2.0f - padding;
    }

}
